package com.h3c.ptability.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eos.common.util.IdWorker;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.h3c.ptability.entity.PtRoleApi;
import com.h3c.ptability.mapper.PtRoleApiMapper;
import com.h3c.ptability.service.IPtRoleApiService;
import com.h3c.ptability.dto.PtRoleApiDTO;
import com.h3c.ptability.utils.InitExampleOrderByUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * Copyright (C), 2020-2020, h3c
 * FileName: PtRoleApiServiceImpl
 *
 * @Author: l22886
 * @Date: 2022-03-14
 * Description: pt_role_api serviceImpl
 * Version: 1.0
 */
@Slf4j
@Service
public class PtRoleApiServiceImpl extends ServiceImpl<PtRoleApiMapper, PtRoleApi> implements IPtRoleApiService {

    @Resource
    private PtRoleApiMapper ptRoleApiMapper;

    

    /**
     * @title: selectFuzzyByDto
     * @description: 模糊查询操作
     * @params: [ dto ]
     * @return: java.util.List
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public List<PtRoleApiDTO> selectFuzzyByDto(PtRoleApiDTO dto) {
        //List<PtRoleApi> list = ptRoleApiMapper.selectByExample(initFuzzyExample(dto));
        List<PtRoleApi> list = ptRoleApiMapper.queryListByFuzzy(JSONObject.parseObject(JSONObject.toJSONString(dto),PtRoleApi.class));
        return JSONArray.parseArray(JSONArray.toJSONString(list), PtRoleApiDTO.class);
    }

    /**
     * @title: selectByDto
     * @description: 精确查询操作
     * @params: [ dto ]
     * @return: java.util.List
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public List<PtRoleApiDTO> selectByDto(PtRoleApiDTO dto) {
        //List<PtRoleApi> list = ptRoleApiMapper.selectByExample(initExample(dto));
        List<PtRoleApi> list = ptRoleApiMapper.queryList(JSONObject.parseObject(JSONObject.toJSONString(dto),PtRoleApi.class));
        return JSONArray.parseArray(JSONArray.toJSONString(list), PtRoleApiDTO.class);
    }

    /**
     * @title: insertDto
     * @description: 插入操作
     * @params: [ dto ]
     * @return: Integer
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public Integer insertDto(PtRoleApiDTO dto) {
        //if (null == dto.getId() || "".equals(dto.getId())) {
        //    String pkId = idWorker.nextId() + "";
        //    dto.setId(pkId);
        //}
        return ptRoleApiMapper.insertPtRoleApi(JSONObject.parseObject(JSON.toJSONString(dto), PtRoleApi.class));
    }

    /**
     * @title: updateDto
     * @description: 修改操作
     * @params: [ dto ]
     * @return: Integer
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public Integer updateDto(PtRoleApiDTO dto) {
        //if (null != dto && null != dto.getId() && !"".equals(dto.getId())) {
            return ptRoleApiMapper.updatePtRoleApi(JSONObject.parseObject(JSON.toJSONString(dto), PtRoleApi.class));
            //return ptRoleApiMapper.updateByPrimaryKey(JSONObject.parseObject(JSON.toJSONString(dto), PtRoleApi.class));
        //} else {
        //    return 0;
        //}
    }

    /**
     * @title: deleteDto
     * @description: 删除操作
     * @params: [ dto ]
     * @return: Integer
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public Integer deleteDto(PtRoleApiDTO dto) {
        //if (null != dto && null != dto.getId() && !"".equals(dto.getId())) {
            return ptRoleApiMapper.deletePtRoleApi(JSONObject.parseObject(JSON.toJSONString(dto), PtRoleApi.class));
            //return ptRoleApiMapper.deleteByPrimaryKey(dto.getId());
        //} else {
        //    return 0;
        //}
    }


    /**
     * @title: initFuzzyExample
     * @description: 初始化 模糊查询 example
     * @params: [ dto ]
     * @return: tk.mybatis.mapper.entity.Example
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    private Example initFuzzyExample(PtRoleApiDTO dto) {
        Example example = InitExampleOrderByUtils.initExampleOrderBy(PtRoleApi.class, dto.getOrderBy());
        Example.Criteria criteriaName = example.createCriteria();
        if (null != dto) {
            if (null != dto.getApiId() && !"".equals(dto.getApiId())) {
                criteriaName.andLike("ApiId", "%" + dto.getApiId() + "%");
            }
            if (null != dto.getRoleId() && !"".equals(dto.getRoleId())) {
                criteriaName.andLike("RoleId", "%" + dto.getRoleId() + "%");
            }
        }
        return example;
    }

    /**
     * @title: initExample
     * @description: 初始化 精确查询 example
     * @params: [ dto ]
     * @return: tk.mybatis.mapper.entity.Example
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    private Example initExample(PtRoleApiDTO dto) {
        Example example = InitExampleOrderByUtils.initExampleOrderBy(PtRoleApi.class, dto.getOrderBy());
        Example.Criteria criteriaName = example.createCriteria();
        if (null != dto) {
            if (null != dto.getApiId() && !"".equals(dto.getApiId())) {
                criteriaName.andEqualTo("ApiId", dto.getApiId());
            }
            if (null != dto.getRoleId() && !"".equals(dto.getRoleId())) {
                criteriaName.andEqualTo("RoleId", dto.getRoleId());
            }
        }
        return example;
    }


}
